
package com.shop.cloud.weixin.admin.controller;

import cn.binarywang.wx.miniapp.api.WxMaOrderShippingService;
import cn.binarywang.wx.miniapp.bean.shop.request.shipping.WxMaOrderShippingInfoUploadRequest;
import cn.binarywang.wx.miniapp.bean.shop.response.WxMaOrderShippingIsTradeManagedResponse;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.common.security.annotation.Inside;
import com.shop.cloud.weixin.admin.config.ma.WxMaConfiguration;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.web.bind.annotation.*;

/**
 * 小程序发货信息管理
 *
 * @author
 * @date 2023-11-23 21:26:35
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/wxmaordershipping")
@Tag(description = "wxmaordershipping", name = "小程序发货信息管理")
public class WxMaOrderShippingController {

	/**
	 * 发货信息录入接口
	 * @param appId
	 * @param request
	 * @return
	 * @throws WxErrorException
	 */
	@Operation(summary = "发货信息录入接口")
	@PostMapping("/inside/upload/{appId}")
	@Inside
	public R upload(@PathVariable("appId") String appId, @RequestBody WxMaOrderShippingInfoUploadRequest request) throws WxErrorException {
		WxMaOrderShippingService wxMaOrderShippingService = WxMaConfiguration.getMaService(appId).getWxMaOrderShippingService();
		WxMaOrderShippingIsTradeManagedResponse wxMaOrderShippingIsTradeManagedResponse = wxMaOrderShippingService.isTradeManaged(appId);
		//查询小程序是否已完成交易结算管理确认
		if(wxMaOrderShippingIsTradeManagedResponse.getErrCode() == 0 && wxMaOrderShippingIsTradeManagedResponse.getTradeManaged()){
			return R.ok(wxMaOrderShippingService.upload(request));
		}
		return R.ok();
	}
}
